package com.attendance.www.hr.utils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class TimeUtils {

	public static void main(String[] args) {
		//System.out.println(compareTime("18:30","07:00"));
		//Date date=getLastDayOfMonth(ToolUtils.formatData("2020-06-01"));
		//System.out.println(ToolUtils.dateToStr(date));
		System.out.println(compareTime("08:52","04:52"));
		System.out.println(compareTime("08:52","08:52"));
		System.out.println(compareTime("08:52","08:53"));
		//System.out.println(compareTime("17:30","22:54"));
		//System.out.println(compareTime("18:59","19:10"));
		
		/*
		 * System.out.println(compareTime("06:59","06:00"));
		 * System.out.println(compareTime("06:59","06:00"));
		 * 
		 * 
		 * System.out.println(differMinutes("17:30","22:54"));
		 * System.out.println(differMinutes("08:30", "17:30"));
		 */
		
		/*getDayOfMonth(new Date());
		getLastDayOfMonth(new Date());
		
		/*System.out.println(compareTime("07:00","07:10"));*/

		
		/*System.out.println(compareTime("07:00","19:00"));
		System.out.println(compareTime("18:00","19:00"));
		System.out.println(compareTime("19:00","19:00"));
		System.out.println(compareTime("19:01","19:00"));*/
		
		//System.out.println(compareTime("19:00","07:00"));
		
		/*System.out.println(compareTime("19:01", "19:00"));
		
		System.out.println("-");

		System.out.println(compareTime("06:01", "07:00"));
		System.out.println(compareTime("07:00", "07:00"));
		System.out.println(compareTime("07:01", "07:00"));*/
		
		//System.out.println(differMinutes("08:00", "16:00"));
		//System.out.println(differMinutes("18:55", "07:01"));
		//System.out.println(differMinutes("08:00", "10:00"));
		//System.out.println(dateDiff("9999-01-01 07:55", "9999-01-01 15:20"));
		//System.out.println(dateDiff("9999-01-01 08:00", "9999-01-01 16:00"));
		//System.out.println(dateDiff("9999-01-01 18:10", "9999-01-02 02:37"));
		
		//System.out.println(dateCalendar(ToolUtils.formatData("2020-07-02"),ToolUtils.formatData("2020-07-01"),ToolUtils.formatData("2020-07-01")));
	}

	/**
	 * 计算时长
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @return
	 */
	public static Integer dateDiff(String startTime, String endTime) {
		// 按照传入的格式生成一个simpledateformate对象
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long diff;
		long day = 0;
		long hour = 0;
		long min = 0;
		Integer minutes =0;
		// 获得两个时间的毫秒时间差异
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			day = diff / nd;// 计算差多少天
			hour = diff % nd / nh + day * 24;// 计算差多少小时
			min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
			// 输出结果
			System.out.println("hour=" + hour + ",min=" + min);
			minutes=Integer.parseInt(hour+"")*60+Integer.parseInt(min+"");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return minutes;
	}

	// 计算时间间隔多少分钟
	public static Integer differMinutes(String fromTime, String toTime) {
		if(StringUtils.isBlank(fromTime)
				|| StringUtils.isBlank(toTime)) {
			return 0;
		}
		String startTime="9999-01-01";
		String endTime="9999-01-01";
		if(compareTime(fromTime,toTime)==1) {
			endTime="9999-01-02";
		}
		startTime=startTime+" "+fromTime;
		endTime=endTime+" "+toTime;
		Integer minutes = dateDiff(startTime,endTime);
		return minutes;
	}

	// 计算结束时间（小时：分钟）
	public static String getEndTime(String fromTime, Integer differMinutes) {
		String[] fromtimes = fromTime.split(":");
		Integer endMinute = Integer.parseInt(fromtimes[0]) * 60 + Integer.parseInt(fromtimes[1]) + differMinutes;
		return String.valueOf(endMinute / 60) + ":" + String.valueOf(endMinute % 60);
	}

	// 只取整数小时，多余的分钟舍弃
	public static Integer getHoursinterval(String fromTime, String toTime) {
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		if (totimes[1].compareToIgnoreCase(fromtimes[1]) > 0) {
			return Integer.parseInt(totimes[0]) - Integer.parseInt(fromtimes[0]);
		} else {
			return Integer.parseInt(totimes[0]) - Integer.parseInt(fromtimes[0]) - 1;
		}
	}

	// 计算小时数，小数点后保留两位
	public static BigDecimal getHours(String fromDate, String fromTime, String toDate, String toTime) {
		Calendar fromCalendar = Calendar.getInstance();
		Calendar toCalendar = Calendar.getInstance();
		String[] fromDates = fromDate.split("-");
		String[] toDates = toDate.split("-");
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		fromCalendar.set(Calendar.YEAR, Integer.parseInt(fromDates[0]));
		fromCalendar.set(Calendar.MONTH, Integer.parseInt(fromDates[1]) - 1);
		fromCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(fromDates[2]));
		fromCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(fromtimes[0]));
		fromCalendar.set(Calendar.MINUTE, Integer.parseInt(fromtimes[1]));
		toCalendar.set(Calendar.YEAR, Integer.parseInt(toDates[0]));
		toCalendar.set(Calendar.MONTH, Integer.parseInt(toDates[1]) - 1);
		toCalendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(toDates[2]));
		toCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(totimes[0]));
		toCalendar.set(Calendar.MINUTE, Integer.parseInt(totimes[1]));
		return new BigDecimal(toCalendar.getTimeInMillis() - fromCalendar.getTimeInMillis())
				.divide(new BigDecimal(1000 * 60 * 60), 2);
	}

	// 计算小时数，小数点后保留两位
	public static BigDecimal getHours(Date fromDate, String fromTime, Date toDate, String toTime) {
		Calendar fromCalendar = Calendar.getInstance();
		Calendar toCalendar = Calendar.getInstance();
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		fromCalendar.setTimeInMillis(fromDate.getTime());
		fromCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(fromtimes[0]));
		fromCalendar.set(Calendar.MINUTE, Integer.parseInt(fromtimes[1]));
		toCalendar.setTimeInMillis(toDate.getTime());
		toCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(totimes[0]));
		toCalendar.set(Calendar.MINUTE, Integer.parseInt(totimes[1]));
		return new BigDecimal(toCalendar.getTimeInMillis() - fromCalendar.getTimeInMillis())
				.divide(new BigDecimal(1000 * 60 * 60), 2);
	}

	/**
	 * 判断两个时间大小
	 * 
	 * @param fromTime
	 * @param toTime
	 * @return 大于：1，小于：-1，相等：0
	 */
	public static Integer compareTime(String fromTime, String toTime) {
		if(StringUtils.isBlank(fromTime)
				|| StringUtils.isBlank(toTime)) {
			return 1;
		}
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		if (Integer.parseInt(fromtimes[0]) > Integer.parseInt(totimes[0])) {
			return 1;
		} else if (Integer.parseInt(fromtimes[0]) < Integer.parseInt(totimes[0])) {
			return -1;
		} else {
			if (Integer.parseInt(fromtimes[1]) > Integer.parseInt(totimes[1])) {
				return 1;
			} else if (Integer.parseInt(fromtimes[1]) < Integer.parseInt(totimes[1])) {
				return -1;
			} else {
				return 0;
			}
		}
	}

	/**
	 * 判断两个时间大小
	 * 
	 * @param fromTime
	 * @param toTime
	 * @return 大于：1，小于：-1，相等：0
	 */
	public static Integer compareDateTime(Date fromDate, String fromTime, Date toDate, String toTime) {
		if (fromDate.compareTo(toDate) == 0) {
			String[] fromtimes = fromTime.split(":");
			String[] totimes = toTime.split(":");
			if (Integer.parseInt(fromtimes[0]) > Integer.parseInt(totimes[0])) {
				return 1;
			} else if (Integer.parseInt(fromtimes[0]) < Integer.parseInt(totimes[0])) {
				return -1;
			} else {
				if (Integer.parseInt(fromtimes[1]) > Integer.parseInt(totimes[1])) {
					return 1;
				} else if (Integer.parseInt(fromtimes[1]) < Integer.parseInt(totimes[1])) {
					return -1;
				} else {
					return 0;
				}
			}
		} else {
			return fromDate.compareTo(toDate);
		}

	}

	// 计算时间间隔，分钟
	public static Integer getMinutes(Date fromDate, String fromTime, Date toDate, String toTime) {
		Calendar fromCalendar = Calendar.getInstance();
		Calendar toCalendar = Calendar.getInstance();
		String[] fromtimes = fromTime.split(":");
		String[] totimes = toTime.split(":");
		fromCalendar.setTimeInMillis(fromDate.getTime());
		fromCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(fromtimes[0]));
		fromCalendar.set(Calendar.MINUTE, Integer.parseInt(fromtimes[1]));
		toCalendar.setTimeInMillis(toDate.getTime());
		toCalendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(totimes[0]));
		toCalendar.set(Calendar.MINUTE, Integer.parseInt(totimes[1]));

		return BigDecimal.valueOf((toCalendar.getTimeInMillis() - fromCalendar.getTimeInMillis()) / (1000 * 60))
				.intValue();
	}

	/**
	 * 判断time是否在from，to之内
	 * 
	 * @param time
	 *            指定日期
	 * @param from
	 *            开始日期
	 * @param to
	 *            结束日期
	 * @return
	 */
	public static boolean dateCalendar(Date time, Date from, Date to) {
		Calendar date = Calendar.getInstance();
		date.setTime(time);
		Calendar after = Calendar.getInstance();
		after.setTime(from);
		Calendar before = Calendar.getInstance();
		before.setTime(to);
		if (date.getTime().compareTo(after.getTime()) >= 0 && date.getTime().compareTo(before.getTime()) <= 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 获取传入日期所在月的第一天
	 * @param date
	 * @return
	 */
	public static Date getDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
        System.out.println(format.format(cal.getTime()));
        return cal.getTime();
    }
	
	/**
	 * 获取传入日期所在月的最后一天
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
        System.out.println(format.format(cal.getTime()));
        return cal.getTime();
    }
	
	/**
	 * 日期减一个月
	 * @param date
	 * @return
	 */
	public static Date getMinusMonth(Date date) {  
		 Calendar calendar = Calendar.getInstance();    
		 calendar.setTime(date);     
		 calendar.add(Calendar.MONTH, -1);//当前时间前去一个月，即一个月前的时间    
		 return calendar.getTime();//获取一年前的时间，或者一个月前的时间    
	}
	
	/**
	 * 日期减一天
	 * @param date
	 * @return
	 */
	public static Date getMinusDay(Date date) {  
		 Calendar calendar = Calendar.getInstance();    
		 calendar.setTime(date);     
		 calendar.add(Calendar.DAY_OF_MONTH, -1);
		 return calendar.getTime();
	}
}
